package com.javaclimb.music.controller;

import com.alibaba.druid.sql.visitor.functions.Concat;
import com.alibaba.fastjson.JSONObject;
import com.javaclimb.music.po.Singer;
import com.javaclimb.music.service.SingerService;
import com.javaclimb.music.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
public class SingerController {
    @Autowired
    private SingerService singerService;

    @RequestMapping(value = "/getSingerList",method = RequestMethod.POST)
    public List<Singer> getSingerList() {
        return singerService.getSingerList();
    }

    @RequestMapping(value = "/CreateSinger",method = RequestMethod.POST)
    public JSONObject CreateSinger(Singer singer) {
        JSONObject jsonObject=new JSONObject();
        boolean b=singerService.CreateSinger(singer);
        if (b){
            jsonObject.put(Consts.CODE,"1");
            jsonObject.put(Consts.NAME,"添加成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.NAME,"添加失败");
        return jsonObject;
    }

    @RequestMapping(value ="/DeleteSingerById" ,method =RequestMethod.POST)
    public JSONObject DeleteSingerById(int id) {
        JSONObject jsonObject=new JSONObject();
        boolean c=singerService.DeleteSingerById(id);
        if (c){
            jsonObject.put(Consts.CODE,"1");
            jsonObject.put(Consts.NAME,"删除成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.NAME,"删除失败");
        return jsonObject;
    }

    @RequestMapping(value = "/UpdateSinger",method =RequestMethod.POST)
    public JSONObject UpdateSinger(Singer singer) {
        JSONObject jsonObject=new JSONObject();
       boolean d=singerService.UpdateSinger(singer);
        if (d){
            jsonObject.put(Consts.CODE,"1");
            jsonObject.put(Consts.NAME,"修改成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.NAME,"修改失败");
        return jsonObject;
    }

    @RequestMapping(value = "/LikeSingerByName",method = RequestMethod.POST)
    public List<Singer> LikeSingerByName(String name) {
        return singerService.LikeSingerByName(name);
    }

    @RequestMapping(value = "/DeletesSinger",method = RequestMethod.GET)
    public JSONObject DeletesSinger(int[] id) {
        JSONObject jsonObject=new JSONObject();
        boolean ds=singerService.DeletesSinger(id);
        if (ds){
            jsonObject.put(Consts.CODE,"1");
            jsonObject.put(Consts.NAME,"删除成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.NAME,"删除失败");
        return jsonObject;
    }

    @RequestMapping(value = "/Singeruploadpic",method = RequestMethod.POST)
    public Object SingerUploadPic(@RequestParam("file")MultipartFile avatorFile,@RequestParam("id") int id)
    {
        JSONObject jsonObject=new JSONObject();
        if (avatorFile.isEmpty()){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"文件上传失败");
            return jsonObject;
        }
//        文件名=当前时间到毫秒+原来的文件名
        String filename=System.currentTimeMillis()+avatorFile.getOriginalFilename();
//        文件路径  System.getProperty("user.dir"):获取到用户系统路径     System.getProperty("file.separator"):分隔符”/“
        String filePath = System.getProperty("user.dir")+System.getProperty("file.separator")+"img"
                +System.getProperty("file.separator")+"singerPic";

        //如果文件路径不存在，新增该路径

        File file1 = new File(filePath);

        if(!file1.exists()){

            file1.mkdirs();

        }

        //实际的文件地址

        File dest = new File(filePath+System.getProperty("file.separator")+filename);

        //存储到数据库里的相对文件地址

        String storeAvatorPath = "/img/singerPic/"+filename;

        try {

            avatorFile.transferTo(dest);

            Singer singer = new Singer();

            singer.setId(id);

            singer.setPic(storeAvatorPath);

            boolean flag = singerService.UpdateSinger(singer);

            if(flag){

                jsonObject.put(Consts.CODE,1);

                jsonObject.put(Consts.MSG,"上传成功");

                jsonObject.put("pic",storeAvatorPath);

                return jsonObject;

            }

            jsonObject.put(Consts.CODE,0);

            jsonObject.put(Consts.MSG,"上传失败");

            return jsonObject;

        } catch (IOException e) {

            jsonObject.put(Consts.CODE,0);

            jsonObject.put(Consts.MSG,"上传失败"+e.getMessage());

        }finally {

            return jsonObject;

        }

    }

    @RequestMapping(value = "/getSingerNum",method = RequestMethod.POST)
    public int getSingerNum() {
        return singerService.getSingerNum();
    }

    }

